package com.genghis.ptas.question;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.classify.entity.Type;
import com.genghis.ptas.classify.entity.TypeValues;
import com.genghis.ptas.classify.service.ClassifyService;
import com.genghis.ptas.question.entity.Answer;
import com.genghis.ptas.question.entity.Question;
import com.genghis.ptas.question.service.AnswerService;
import com.genghis.ptas.question.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Controller
@Transactional
@RequestMapping("question/manager")
public class QuestionManagerController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private AnswerService answerService;

    @RequestMapping(value = "listAllQuestion", method = {RequestMethod.GET, RequestMethod.POST})
    public String listAllQuestion() {
        return "backstage/question/listAllQuestionDo";
    }

    @RequestMapping(value = "listAllQuestionDo", method = RequestMethod.POST)
    @ResponseBody
    public Page listAllQuestionDo(PageParameter pageParameter, Question question) {
        return questionService.findAllQuestionPageList(pageParameter, question);
    }

    @RequestMapping(value = "addAsk", method = RequestMethod.GET)
    public String addAsk(HttpServletRequest request) {
        List<Type> typeList = classifyService.findAllTypeList();
        List<TypeValues> typeValuesList = classifyService.findAllTypeValueList();
        request.setAttribute("typeList", typeList);
        request.setAttribute("typeValuesList", typeValuesList);
        return "backstage/question/addQuestionDo";
    }

    @RequestMapping(value = "editAsk", method = RequestMethod.GET)
    public String editAsk(HttpServletRequest request) {
        String id = request.getParameter("id");
        Question question = questionService.findQuestionById(Integer.parseInt(id));
        List<Type> typeList = classifyService.findAllTypeList();
        List<TypeValues> typeValuesList = classifyService.findAllTypeValueList();
        request.setAttribute("typeList", typeList);
        request.setAttribute("typeValuesList", typeValuesList);
        request.setAttribute("question", question);
        return "backstage/question/editQuestionDo";
    }

    @RequestMapping(value = "listAllAnswer", method = {RequestMethod.GET, RequestMethod.POST})
    public String listAllAnswer(HttpServletRequest request) {
        String id = request.getParameter("id");
        request.setAttribute("questionId", id);
        return "backstage/question/listAllAnswerDo";
    }

    @RequestMapping(value = "listAllAnswerDo", method = RequestMethod.POST)
    @ResponseBody
    public Page listAllAnswerDo(PageParameter pageParameter, Answer answer) {
        return questionService.findAllAnswerPageList(pageParameter, answer);
    }

    @RequestMapping(value = "addAnswer", method = RequestMethod.GET)
    public String addAnswer(HttpServletRequest request) {
        String questionId = request.getParameter("questionId");
        request.setAttribute("questionId", questionId);
        List<Type> typeList = classifyService.findAllTypeList();
        List<TypeValues> typeValuesList = classifyService.findAllTypeValueList();
        request.setAttribute("typeList", typeList);
        request.setAttribute("typeValuesList", typeValuesList);
        return "backstage/question/addAnswerDo";
    }

    @RequestMapping(value = "editAnswer", method = RequestMethod.GET)
    public String editAnswer(HttpServletRequest request) {
        String answerId = request.getParameter("answerId");
        String questionId = request.getParameter("questionId");
        Answer answer = answerService.findAnswerById(Integer.parseInt(answerId));
        List<Type> typeList = classifyService.findAllTypeList();
        List<TypeValues> typeValuesList = classifyService.findAllTypeValueList();
        request.setAttribute("typeList", typeList);
        request.setAttribute("typeValuesList", typeValuesList);
        request.setAttribute("answer", answer);
        request.setAttribute("questionId", questionId);
        return "backstage/question/editAnswerDo";
    }
}
